<template>
    <a-card  :title="title" :bordered="false">
      <a-list>
        <a-list-item :key="index" v-for="(item, index) in dataSource">
          <a-list-item-meta  style="padding: 0px">
            <div slot="title">
              <h3 style="margin: 0px" @click="handleDetail(item)"><span v-if="type=='0'" style="color: red">*</span>&nbsp;<a-icon type="home" />&nbsp;&nbsp;{{ item.titile }}</h3>&nbsp;
              <span  >发布人：{{ item.sender}}</span>
            </div>
            <div slot="description">{{ item.sendTime }}</div>
          </a-list-item-meta>
        </a-list-item>
      </a-list>
      <a-pagination
        style="float: right;"
        size="small"
        :pageSize="ipagination.pageSize"
        :current="ipagination.current"
        :defaultCurrent="ipagination.current"
        :total="ipagination.total"
        :pageSizeOptions="ipagination.pageSizeOptions"
        @change="setcurrent" />
      <sysAnnouncement-modal ref="modalForm" @ok="modalFormOk"></sysAnnouncement-modal>
    </a-card>
</template>

<script>
import {mapGetters} from "vuex";
import {getAction, httpAction} from "@api/manage";
import SysAnnouncementModal from "@views/system/modules/SysAnnouncementModal";

export default {
  name: "informList",
  components: {SysAnnouncementModal},
  props:['title','type','pageSize'],
  data() {
    return {
      /* 分页参数 */
      ipagination:{
        current: 1,
        pageSize: this.pageSize,
        pageSizeOptions: ['10', '20', '30'],
        total: 6
      },
      url:{
        list: "/sys/annountCement/listByPage",
        add:"/sysUserInform/sysUserInform/add",
      },
      dataSource:[],
    }
  },
  created() {
    this.loadData();
  },
  mounted(){
    const timer = setInterval(()=>{
      this.getloadData();
    },60*1000);
    this.$once('hook:beforeDestroy', ()=>{
      clearInterval(timer)
    })
  },
  methods: {
    ...mapGetters(["userInfo"]),
    setcurrent(page,pageSize){
      this.ipagination.current=page;
      this.ipagination.pageSize=pageSize;
      this.loadData();
    },
    modalFormOk() {
      // 新增/修改 成功时，重载列表
      this.loadData();
    },
    handleDetail:function(record){
      this.$refs.modalForm.edit(record);
      this.$refs.modalForm.title="详情";
      this.$refs.modalForm.disableSubmit = true;
      let that=this;
      if(this.type=='0'){
       let formData ={informId:record.id};
        httpAction(this.url.add,formData,'post').then((res)=>{
          that.loadData(1);
        })
      }
    },
    getloadData(){
      var params = {
        sendStatus:1,
        type:this.type,
        username: this.userInfo().username,
        pageNo:this.ipagination.current,
        pageSize:this.ipagination.pageSize,
      };//查询条件
      let that=this;
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          if(res.result.total>this.ipagination.total){
            that.loadData(1);
          }
        }
      })
    },
    loadData(arg) {
      if(!this.url.list){
        this.$message.error("请设置url.list属性!")
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1;
      }
      var params = {
        sendStatus:1,
        type:this.type,
        username: this.userInfo().username,
        pageNo:this.ipagination.current,
        pageSize:this.ipagination.pageSize,
      };//查询条件
      getAction(this.url.list, params).then((res) => {
        console.log(this.type+"res-->",res.result.total);
        if (res.success) {
          this.dataSource = res.result.records;
          this.ipagination.total = res.result.total;
        }
      })
    },
  }
}
</script>

<style scoped>
.contText{
  width: 100rem;
  overflow: hidden;/*内容超出后隐藏*/
  text-overflow: ellipsis;/* 超出内容显示为省略号*/
  white-space: nowrap;/*文本不进行换行*/
}
</style>